<template>
  <nav class="navbar">
    <div class="navbar-container">
      <!-- Logo和品牌名称 -->
      <div class="navbar-brand">
        <router-link to="/" class="brand-link">
          <span class="brand-name">校园活动管理系统</span>
        </router-link>
      </div>
      
      <!-- 导航链接 - 桌面端 -->
      <div class="navbar-nav">
        <router-link to="/" class="nav-link" active-class="active">首页</router-link>
        <router-link to="/activities" class="nav-link" active-class="active">活动列表</router-link>
        <router-link to="/activity-edit" class="nav-link" active-class="active">发布活动</router-link>
      </div>
      
      <!-- 用户区域 -->
      <div class="navbar-user">
        <!-- 未登录状态 -->
        <template v-if="!isAuthenticated">
          <router-link to="/login" class="user-link">登录</router-link>
          <router-link to="/register" class="user-link">注册</router-link>
        </template>
        
        <!-- 已登录状态 -->
        <template v-else>
          <el-dropdown @command="handleUserMenu">
            <div class="user-profile">
              <img :src="userAvatar || '/default-avatar.png'" alt="用户头像" class="avatar" />
              <span class="username">{{ userName }}</span>
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
            <el-dropdown-item command="profile">个人中心</el-dropdown-item>
            <el-dropdown-item command="activities">我的活动</el-dropdown-item>
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
        
        <!-- 移动端菜单 -->
        <button class="mobile-menu-btn" @click="toggleMobileMenu">
          <el-icon v-if="!isMobileMenuOpen"><menu /></el-icon>
          <el-icon v-else><close /></el-icon>
        </button>
      </div>
    </div>
    
    <!-- 移动端菜单 -->
    <div class="mobile-menu" v-if="isMobileMenuOpen">
      <router-link to="/" class="mobile-link" @click="closeMobileMenu">首页</router-link>
      <router-link to="/activities" class="mobile-link" @click="closeMobileMenu">活动列表</router-link>
      <router-link to="/activity-edit" class="mobile-link" @click="closeMobileMenu">发布活动</router-link>
      <template v-if="isAuthenticated">
        <router-link to="/profile" class="mobile-link" @click="closeMobileMenu">个人中心</router-link>
        <router-link to="/my-activities" class="mobile-link" @click="closeMobileMenu">我的活动</router-link>
        <div class="mobile-link" @click="handleLogout">退出登录</div>
      </template>
      <template v-else>
        <router-link to="/login" class="mobile-link" @click="closeMobileMenu">登录</router-link>
        <router-link to="/register" class="mobile-link" @click="closeMobileMenu">注册</router-link>
      </template>
    </div>
  </nav>
</template>

<script>
import { computed, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowDown, Close } from '@element-plus/icons-vue'

export default {
  name: 'NavBar',
  components: {
    ArrowDown,
    Close
  },
  setup() {
    const router = useRouter()
    
    // 计算属性
    const isAuthenticated = computed(() => {
      const token = localStorage.getItem('token')
      return !!token
    })
    const userName = computed(() => {
      const userInfo = localStorage.getItem('userInfo')
      if (userInfo) {
        try {
          const user = JSON.parse(userInfo)
          return user.username || user.name || ''
        } catch {
          return ''
        }
      }
      return ''
    })
    const userAvatar = computed(() => {
      const userInfo = localStorage.getItem('userInfo')
      if (userInfo) {
        try {
          const user = JSON.parse(userInfo)
          return user.avatar || '/default-avatar.png'
        } catch {
          return '/default-avatar.png'
        }
      }
      return '/default-avatar.png'
    })
    const isMobileMenuOpen = computed(() => mobileMenuOpen.value)
    
    // 方法
    // 移动端菜单状态
    const mobileMenuOpen = ref(false)
    
    const toggleMobileMenu = () => {
      mobileMenuOpen.value = !mobileMenuOpen.value
    }
    
    const closeMobileMenu = () => {
      mobileMenuOpen.value = false
    }
    
    const handleUserMenu = (command) => {
      switch (command) {
        case 'profile':
          router.push('/profile')
          break
        case 'activities':
          router.push('/my-activities')
          break
        case 'logout':
          handleLogout()
          break
      }
    }
    
    const handleLogout = async () => {
      try {
        // 直接清除本地存储的认证信息
        localStorage.removeItem('token')
        localStorage.removeItem('userInfo')
        
        // 调用退出API（可选，如果后端有退出接口）
        // await logout()
        
        ElMessage.success('退出成功')
        router.push('/login')
        closeMobileMenu()
      } catch (error) {
        ElMessage.error('退出失败')
      }
    }
    
    return {
      isAuthenticated,
      userName,
      userAvatar,
      isMobileMenuOpen,
      toggleMobileMenu,
      closeMobileMenu,
      handleUserMenu,
      handleLogout
    }
  }
}
</script>

<style scoped>
.navbar {
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.navbar-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
}

.navbar-brand {
  flex-shrink: 0;
}

.brand-link {
  display: flex;
  align-items: center;
  text-decoration: none;
}

.brand-name {
  font-size: 20px;
  font-weight: 600;
  color: #409eff;
}

.navbar-nav {
  display: flex;
  align-items: center;
  margin: 0 auto;
}

.nav-link {
  padding: 0 20px;
  height: 60px;
  line-height: 60px;
  color: #606266;
  text-decoration: none;
  transition: all 0.3s;
}

.nav-link:hover,
.nav-link.active {
  color: #409eff;
}

.navbar-user {
  display: flex;
  align-items: center;
}

.user-link {
  padding: 0 15px;
  color: #606266;
  text-decoration: none;
  transition: color 0.3s;
}

.user-link:hover {
  color: #409eff;
}

.user-profile {
  display: flex;
  align-items: center;
  padding: 0 10px;
  cursor: pointer;
  height: 60px;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  margin-right: 8px;
  object-fit: cover;
}

.username {
  color: #606266;
  margin-right: 4px;
}

.mobile-menu-btn {
  display: none;
  background: none;
  border: none;
  font-size: 20px;
  color: #606266;
  cursor: pointer;
  padding: 8px;
}

.mobile-menu {
  display: none;
  position: absolute;
  top: 60px;
  left: 0;
  right: 0;
  background: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.mobile-link {
  display: block;
  padding: 15px 20px;
  color: #606266;
  text-decoration: none;
  border-bottom: 1px solid #f0f0f0;
  transition: all 0.3s;
}

.mobile-link:hover {
  background: #f5f7fa;
  color: #409eff;
}

@media (max-width: 768px) {
  .navbar-nav {
    display: none;
  }
  
  .user-profile,
  .user-link {
    display: none;
  }
  
  .mobile-menu-btn {
    display: block;
  }
  
  .mobile-menu {
    display: block;
  }
}
</style>